require 'json'

module IfItWerent4AR
  module Serialization
    def self.included(base)
      base.extend(ClassMethods)
    end

    def serialize
      attrs_arr = []
      self.class.attributes.map do |attr|
        attrs_arr << send("#{attr}")
      end
      attrs_arr.to_s
    end

    module ClassMethods
      def deserialize(serialized)
        attrs_arr = eval(serialized)
        attrs = {}
        attributes_with_types.each_with_index do |(attr, type), index|
          attrs[attr] = (type == 'integer' ? attrs_arr[index].to_s.to_i : attrs_arr[index])
        end
        new(attrs)
      end
    end
  end
end
